# convert trace from output of ffprobe
#
# ffprobe -print_format json -show_frames input.mp4 > input.json

import argparse
import json
import os


def convert_trace(trace_file_name: str):
    result_trace_name = os.path.join(os.path.splitext(trace_file_name)[0] + ".txt")
    with open(trace_file_name, "r") as f:
        trace = json.load(f)["frames"]
        with open(result_trace_name, "w") as res:
            for frame in trace:
                res.write(
                    "{} {} {} {}\n".format(
                        frame["pkt_duration_time"],
                        500,
                        frame["pkt_size"],
                        1 if frame["pict_type"] == "I" else 2,
                    )
                )


parser = argparse.ArgumentParser(description="Convert trace from ffprobe")
parser.add_argument(
    "traces",
    metavar="FILE",
    type=str,
    nargs="+",
    help="trace file generated by ffprobe",
)

if __name__ == "__main__":
    args = parser.parse_args()
    for trace in args.traces:
        convert_trace(trace)
